package com.efounder.builder.base.util;

import java.util.Date;
import java.util.Map;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.base.data.ESPRowSet;

/**
 * @author Skyline
 *
 */
public final class MapUtil {
  /**
   *
   * @param map
   * @param keyName
   * @param number
   */
  public static void putNumber(Map map, String keyName, Number value) {
    putObject(map,keyName,value);
  }
  /**
   *
   * @param map Map
   * @param keyName String
   * @param defValue Number
   * @return Number
   */
  public static Number getNumber(Map map, String keyName, Number defValue) {
    return getNumber(map,keyName,defValue,null);
  }
  /**
   *
   * @param map
   * @param keyName
   * @param defNum
   * @return
   */
  public static Number getNumber(Map map, String keyName, Number defValue,ESPRowSet depRowSet) {
    Object value = getObject(map,keyName,defValue,depRowSet);
    if (value instanceof Number && value != null) {
      return (Number) value;
    }
    else {
      return defValue;
    }
  }

  /**
   *
   * @param map
   * @param keyName
   * @param value
   */
  public static void putString(Map map, String keyName, String value) {
    putObject(map,keyName,value);
  }
  /**
   *
   * @param map Map
   * @param keyName String
   * @param defValue String
   * @return String
   */
  public static String getString(Map map, String keyName, String defValue) {
    return getString(map,keyName,defValue,null);
  }
  /**
   *
   * @param map
   * @param keyName
   * @param defValue
   * @return
   */
  public static String getString(Map map, String keyName, String defValue,ESPRowSet depRowSet) {
    Object value = getObject(map,keyName,defValue,depRowSet);
    if (value instanceof String && value != null) {
      return (String) value;
    }
    else {
      return defValue;
    }
  }

  public static void putBoolean(Map map, String keyName, Boolean value) {
    putObject(map,keyName,value);
  }
  /**
   *
   * @param map Map
   * @param keyName String
   * @param defValue Boolean
   * @return Boolean
   */
  public static Boolean getBoolean(Map map, String keyName, Boolean defValue) {
    return getBoolean(map,keyName,defValue,null);
  }
  /**
   *
   * @param map
   * @param keyName
   * @param defValue
   * @return
   */
  public static Boolean getBoolean(Map map, String keyName, Boolean defValue,ESPRowSet depRowSet) {
    Object value = getObject(map,keyName,defValue,depRowSet);
    if (value instanceof Boolean && value != null) {
      return (Boolean) value;
    }
    else {
      return defValue;
    }
  }

  /**
   *
   * @param map
   * @param keyName
   * @param value
   */
  public static void putDate(Map map, String keyName, Date value) {
    putObject(map,keyName,value);
  }
  /**
   *
   * @param map Map
   * @param keyName String
   * @param defValue Date
   * @return Date
   */
  public static Date getDate(Map map, String keyName, Date defValue) {
    return getDate(map,keyName,defValue,null);
  }
  /**
   *
   * @param map
   * @param keyName
   * @param defValue
   * @return
   */
  public static Date getDate(Map map, String keyName, Date defValue,ESPRowSet depRowSet) {
    Object value = getObject(map,keyName,defValue,depRowSet);
    if (value instanceof Date && value != null) {
      return (Date) value;
    }
    else {
      return defValue;
    }
  }

  /**
   *
   * @param map
   * @param keyName
   * @param value
   */
  public static void putObject(Map map, String keyName, Object value) {
    if (map == null) {
      return;
    }
    if(value==null)
      map.remove(keyName);
    else
      map.put(keyName, value);
  }
  public static Object getObject(Map map, String keyName, Object defValue) {
    return getObject(map,keyName,defValue,null);
  }
  /**
   *
   * @param map
   * @param keyName
   * @param defValue
   * @return
   */
  public static Object getObject(Map map, String keyName, Object defValue,ESPRowSet depRowSet) {
    if (map == null) {
      return defValue;
    }
    Object value = map.get(keyName);
    if (value instanceof Object && value != null) {
      return value;
    }
    else {
      if ( depRowSet != null ) {
        return depRowSet.getObject(keyName,defValue);
      }
      return defValue;
    }
  }
}
